using System;
using System.Collections.Generic;
using System.Text;

namespace ghostDBscript.Engine
{
    public static class ScriptEngine
    {
        /// <summary>
        /// Processes incoming ghostghostDBscript and returns DDL for SQL Server.
        /// </summary>
        /// <remarks>
        /// Someday this method shall return DDL types other than SQL Server.s
        /// </remarks>
        /// <param name="script"></param>
        /// <returns></returns>
        public static List<Commands.ICommand> Process(string script)
        {
            List<Commands.ICommand> _commands = new List<ghostDBscript.Engine.Commands.ICommand>();
            System.IO.StringReader stringReader = new System.IO.StringReader(script);
            string line = string.Empty;
            Commands.ICommand parentCommand = null;

            while (line != null)
            {
                line = stringReader.ReadLine();
                Commands.ICommand command = GetCommand(line);

                if (command != null)
                {
                    if (parentCommand != null)
                    {
                        if (parentCommand.GetType() == typeof(Commands.TableCreate))
                            ((Commands.TableCreate)parentCommand).Children.Add(command);
                    }
                    else
                        if (command.GetType() == typeof(Commands.Connect) || command.GetType() == typeof(Commands.TableCreate))
                        {
                            parentCommand = command;
                            _commands.Add(command);
                        }

                    if (command.GetType() == typeof(Commands.TableCreate))
                        parentCommand = command;
                }
            }

            return _commands;
        }

        public static string ProcessString(string script)
        {
            StringBuilder america = new StringBuilder();

            List<Commands.ICommand> done = Process(script);

            foreach (Commands.ICommand command in done)
                america.AppendLine(command.ToCommandText());

            return america.ToString();
        }

        private static Commands.ICommand GetCommand(string line)
        {
            Commands.ICommand command = null;

            if (line != null)
            {
            line = line.Trim();

            if (line.Length > 0)
            {
                switch (line.Substring(0, 1))
                {
                    case ".": command = new Commands.TableCreate(line.Substring(2));                break;
                    case "+": command = new Commands.ColumnAdd(line.Substring(2));                  break;
                    case "#": command = new Commands.Connect();                                     break;
                    default : break;
                }
            }

        }
        return command;
        }
    }
}
